home *** CD-ROM | disk | FTP | other *** search
/ Champak 29 / Volume 29 - JOGO DISK .iso / Games / jungle_adventure.swf / scripts / __Packages / BouncingObject.as next >
Text File  |  2006-11-29  |  4KB  |  136 lines

  1. class BouncingObject extends SSObject
  2. {
  3.    var assetID = "Spider";
  4.    var healthValue = -0.2;
  5.    var classID = SSGlobal.CLSID_MOBILEOBJECT;
  6.    var collisionMask = SSGlobal.CLSID_SHAPE | SSGlobal.CLSID_MAINCHAR;
  7.    var jumpStrength = 300;
  8.    var gravity = SSGlobal.GRAVITY * 0.5;
  9.    var vulnerable = false;
  10.    var contactTimeAllow = 1;
  11.    var contactTime = 0;
  12.    var collision = false;
  13.    var immobilized = 0;
  14.    var immobilizeTime = 5;
  15.    var currentAnimation = 1;
  16.    var editor_isItem = true;
  17.    var editor_name = "BouncingObject";
  18.    var editor_args_names = ["healthValue","jumpStrength"];
  19.    var editor_args_values = [BouncingObject.prototype.healthValue,BouncingObject.prototype.jumpStrength];
  20.    var editor_args_types = ["number","number"];
  21.    var editor_args_options = [[-1,1,0.01],[0,600,50]];
  22.    var editor_args_descriptions = ["",""];
  23.    var editor_args_mode = [0,0];
  24.    var editor_args_component = ["NumericStepper","NumericStepper"];
  25.    function BouncingObject()
  26.    {
  27.       super();
  28.    }
  29.    function onAddToWorld()
  30.    {
  31.       this.originX = this.x;
  32.       this.originY = this.y;
  33.    }
  34.    function onAddToScene()
  35.    {
  36.       this.target.gotoAndStop(this.currentAnimation);
  37.       this.velocity.x = this.velocity.y = 0;
  38.       this.getUpdates();
  39.    }
  40.    function onRemoveFromScene()
  41.    {
  42.       this.cancelUpdates();
  43.       this.moveTo(this.originX,this.originY,0);
  44.       this.contact = null;
  45.    }
  46.    function update(elapsed)
  47.    {
  48.       if(this.immobilized)
  49.       {
  50.          if(!(this.immobilized = Math.max(this.immobilized - elapsed,0)))
  51.          {
  52.             this.setAnimation("restore");
  53.          }
  54.       }
  55.       if(!this.contact)
  56.       {
  57.          this.velocity.y += this.gravity * elapsed;
  58.          this.collision = false;
  59.          this.checkCollisions(elapsed);
  60.          this.moveBy(this.velocity.x * elapsed,this.velocity.y * elapsed,0);
  61.       }
  62.       else if(!this.immobilized && (this.contactTime = Math.max(this.contactTime - elapsed,0)) == 0)
  63.       {
  64.          this.velocity.y = - this.jumpStrength;
  65.          this.setAnimation("jump");
  66.          GameSound.playSound("spring");
  67.          this.contact = null;
  68.       }
  69.    }
  70.    function setContact(edge)
  71.    {
  72.       this.contact = edge;
  73.       this.contactTime = this.contactTimeAllow;
  74.    }
  75.    function checkCollision(obj)
  76.    {
  77.       var _loc3_ = undefined;
  78.       switch(obj.classID & 4294901760)
  79.       {
  80.          case SSGlobal.CLSID_SHAPE:
  81.             if(!this.contact && (_loc3_ = SSCollision.sweepSphereToStaticShape(this,obj)))
  82.             {
  83.                var _loc4_ = this.velocity.__get__length();
  84.                if(!this.immobilized)
  85.                {
  86.                   this.setAnimation("still");
  87.                }
  88.                this.setContact(_loc3_.edge);
  89.                this.velocity.x = 0;
  90.                this.velocity.y = 0;
  91.                this.collision = true;
  92.             }
  93.             break;
  94.          case SSGlobal.CLSID_MAINCHAR:
  95.             if(SSCollision.sweepSphereToSphere(this,obj,true))
  96.             {
  97.                return this.onCollision(obj);
  98.             }
  99.             break;
  100.       }
  101.    }
  102.    function immobilize()
  103.    {
  104.       this.velocity.y = -100;
  105.       this.velocity.x = 0;
  106.       this.contact = null;
  107.       GameSound.playSound("enemyhit");
  108.       this.setAnimation("Immobile");
  109.       this.immobilized = this.immobilizeTime;
  110.    }
  111.    function setAnimation(anim)
  112.    {
  113.       this.target.gotoAndStop(this.currentAnimation = anim);
  114.    }
  115.    function onCollision(obj)
  116.    {
  117.       if(!this.inScene || this.immobilized)
  118.       {
  119.          return undefined;
  120.       }
  121.       if(obj.active && (obj.classID & 4294901760) == SSGlobal.CLSID_MAINCHAR)
  122.       {
  123.          if(obj.classID == SSGlobal.CLSID_VEHICLE || obj.shield || obj.y < this.y && obj.velocity.y >= 0)
  124.          {
  125.             this.immobilize();
  126.             obj.velocity.y *= -0.5;
  127.             obj.velocity.x *= 1.1;
  128.          }
  129.          else if(!this.immobilized)
  130.          {
  131.             obj.shiftHealth(this.healthValue,this);
  132.          }
  133.       }
  134.    }
  135. }
  136.